home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / pcscheme / geneva / sources.exe / SOURCES / FONTS / MAKEH.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-21  |  4.0 KB  |  180 lines

  1. /*
  2. GET HERSHEY FONTS
  3. */
  4.  
  5. #include    <stdio.h>
  6. #include    <stdlib.h>
  7. #include    <string.h>
  8. #include    <ctype.h>
  9. #include    "font.h"
  10.  
  11. #define    GLYPHNUM    256
  12.  
  13. int    offset[GLYPHNUM];
  14. char    width[GLYPHNUM];
  15. unsigned data[25000];
  16.  
  17. unsigned encode( int x, int y, int line )
  18. {
  19.     union    {
  20.         struct
  21.         {
  22.             signed    xoff  : 7;
  23.             unsigned flag1 : 1;
  24.             signed    yoff  : 7;
  25.             unsigned flag2 : 1;
  26.         }    cword;
  27.         unsigned u;
  28.     }    data;
  29.     data.cword.xoff = x;
  30.     data.cword.yoff = y;
  31.     data.cword.flag1 = 1;
  32.     data.cword.flag2 = line;
  33.  
  34.     return    data.u;
  35. }
  36.  
  37. int    process( char *line, int glyph, int req, char &capitol, char &descender, int &dp )
  38. {
  39.     int    first, num, strokes, ladj, radj, y;
  40.     char    tmp[1000];
  41.  
  42.     strcpy( tmp, line ); tmp[5] = ' ';
  43.     strcpy( tmp+6, line+5 ); tmp[9] = 0;
  44.  
  45.     sscanf( tmp, "%d %d", &num, &strokes );
  46.     if( req != num )
  47.         return    0;    /* done here */
  48.     fprintf( stderr, "Glyph %d\r", num );
  49.  
  50.     if( offset[glyph] )
  51.     {
  52.         fprintf( stderr, "\t\t\aGlyph %d overwritten (by HERSHEY %d)\r", glyph, num );
  53.         exit( 1 );
  54.     }
  55.  
  56.     line += 8;        /* skip the counts */
  57.     ladj = *line++ - 'R';
  58.     radj = *line++ - 'R';
  59.     width[glyph] = radj - ladj;
  60.     offset[glyph] = 2*dp;
  61.  
  62.     first = strokes-1;
  63.     while( --strokes > 0 )
  64.     {
  65.         if( *line == ' ')
  66.         {
  67.             data[dp++] = encode( line[2] - ladj - 'R',
  68.                 y = '[' - line[3], 0 );
  69.             line += 4;
  70.             strokes--;
  71.         } else {
  72.             data[dp++] = encode( line[0] - ladj - 'R',
  73.                 y = '[' - line[1], strokes!=first );
  74.             line += 2;
  75.         }
  76.         if( y < descender )
  77.             descender = y;
  78.         if( y > capitol )
  79.             capitol = y;
  80.     }
  81.     data[dp++] = encode( radj - ladj, 0, 0 );/* move to new pos */
  82.     data[dp++] = 0;        /* mark end */
  83.     return    1;        /* continue */
  84. }
  85.  
  86. int    getarequest( int &glyph, int &num )
  87. {
  88.     static    gto = -1, _glyph = 0, _num;
  89.     char    line[200];
  90.  
  91.     if( _glyph >= gto )
  92.     {
  93.         gto = -1;
  94.         glyph = gto = 0;
  95.         do
  96.             if( gets( line ) != line )
  97.                 return    0;
  98.         while( sscanf( line, " %c", &num ) != 1 );
  99.  
  100.         if( sscanf( line, "%d \\%d-\\%d", &num, &glyph, >o ) != 3 )
  101.         if( sscanf( line, "%d %c-%c", &num, &glyph, >o ) != 3 )
  102.         if( sscanf( line, "%d \\%d", &num, &glyph ) != 2 )
  103.         if( sscanf( line, "%d %c", &num, &glyph ) != 2 )
  104.             return    0;
  105.         _glyph = glyph;
  106.         _num = num;
  107.         return    1;
  108.     }
  109.     glyph = ++_glyph;
  110.     num = ++_num;
  111.     return    1;
  112. }
  113.  
  114. int    main( int argc, char **argv )
  115. {
  116.     FILE    *in, *out;
  117.     char    line[1000], outname[10];
  118.     int    glyph, stroke, i, num;
  119.     HEADER    h;
  120.     struct {
  121.         char    text[60];
  122.         FHEADER    h;
  123.     }    f;
  124.  
  125.     if( argc < 2 )
  126.         return    1;
  127.  
  128.     if( (in = fopen("HERSHEY.DAT", "rt")) == NULL )
  129.         return    1;
  130.  
  131.     h.sig = SIGNATURE;
  132.     h.mystery = 0;
  133.     h.scan_flag = 0;
  134.     h.org_to_base = 0;
  135.     h.unused = 0;        /* still unset: nchrs, first, cdefs, org2cap, org2dec */
  136.  
  137.     strcpy( f.text, "PK\b\bHERSHEY fonts, (C) 1993 by Larry Bartholdi & his gang\r\n\032");
  138.     f.h.header_size = Prefix_Size;
  139.     f.h.font_major = 1, f.h.font_minor = 0;
  140.     f.h.min_major = 1, f.h.min_minor = 0;
  141.  
  142.     for( i = 0; argv[1][i]; i++ )
  143.         argv[1][i] = toupper( argv[1][i] );
  144.     strncpy( f.h.font_name, argv[1], 4 );
  145.     strncpy( h.fntname, argv[1], 4 );
  146.                 /* still unset: size */
  147.     sprintf( outname, "%s.CHR", argv[1] );
  148.     out = fopen( outname, "wb");
  149.     for( i = 0; i < GLYPHNUM; i++ )
  150.         width[i] = offset[i] = 0;
  151.     i = 0;
  152.     data[i++] = 0;
  153.     h.org_to_cap = h.org_to_dec = 0;
  154.  
  155.     fgets( line, 1000, in );    /* get the machine rolling */
  156.     
  157.     while( getarequest( glyph, num ) )
  158.     {
  159.         while( !process( line, glyph, num, h.org_to_cap, h.org_to_dec, i ) )
  160.         if( !fgets( line, 1000, in ) )
  161.         {
  162.             fprintf( stderr, "\t\t\aCan't process glyph %d (HERSHEY %d)\r", glyph, num );
  163.             exit( 2 );
  164.         }
  165.     }
  166.     for( h.first = 0; h.first != -1 && !offset[h.first]; h.first++ );
  167.     for( h.nchrs = GLYPHNUM-h.first; h.nchrs >= h.first && !offset[h.nchrs+h.first-1]; h.nchrs-- );
  168.     f.h.font_size = sizeof h + 3*h.nchrs + 2*i;
  169.     h.cdefs = sizeof h + 3*h.nchrs;
  170.  
  171.     fwrite( &f, Prefix_Size, 1, out );
  172.     fwrite( &h, sizeof h, 1, out );
  173.     fwrite( offset+h.first, 2 * h.nchrs, 1, out );
  174.     fwrite( width+h.first, h.nchrs, 1, out );
  175.     fwrite( data, 2*i, 1, out );
  176.     fclose( out );
  177.     fclose( in );
  178.     return    0;
  179. }
  180.